<?php

class TaskAssignmentDao extends Dao {

    function __construct() {
        parent::__construct();
        $this->table = 'taskassignment';
    }
    
    public function insertObject($obj){
        $result = DB::insert(DB::raw("INSERT INTO taskassignment ( task_id, user_id) ".
                    "VALUES ('".$obj->getTask()->getId()."' ".
                            ", '".$obj->getUser()->getId()."' ".
                            ")"));
            if (!$result) { return null; }
        
        return DB::getPdo()->lastInsertId();
    }
    public function updateObject($obj, $old_id){
        DB::update(DB::raw("UPDATE taskassignment SET task_id='".$obj->getTask()->getId()."'"
                . ", user_id='".$obj->getUser()->getId()."'"
                . " WHERE id='".$old_id."'"));
    
        return true;
    }
    
    public function deleteObject($id){
        DB::delete(DB::raw("DELETE FROM taskassignment where id='".$id."'"));
        
        return true;
    }
    
    public function getObject($id){
        $result = DB::select(DB::raw("SELECT * FROM taskassignment where id='".$id."'"));

        if (!$result) { return null; }
        return $this->toObject($result[0]);
    }
    
    public function getList($criteria = NULL){
        $result = DB::select(DB::raw("SELECT * FROM taskassignment"));
        $list = array();
        foreach ($result as $rowdata){
            $list[] = $this->toObject($rowdata);
        }
            
        return $list;
    }
    
    public function getListByUser($userId){
        try {
            $result = parent::where('user_id','=',$userId)->get();
            $list = array();
            foreach($result as $rowdata){
                $list[] = $this->toObject($rowdata);
            }
            return $list;
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
        }
    }
    
    public function getListByCreator($createdBy){
        try {
            $result = parent::where('created_by','=',$createdBy)->get();
            $list = array();
            foreach($result as $rowdata){
                $list[] = $this->toObject($rowdata);
            }
            return $list;
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
        }
    }
    
     public function getListByTaskId($taskId){
        try {
            $result = parent::where('task_id','=',$taskId)->get();
            $list = array();
            foreach($result as $rowdata){
                $list[] = $this->toObject($rowdata);
            }
            return $list;
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
        }
    }
    
    
    public function toObject($rowdata=null) {
        $taskassignment = new TaskAssignment();
        $taskassignment->setId($rowdata->id);
        
        $user = new User();
        $user->setId($rowdata->user_id);
        $user->setIsLoaded(false);
        $taskassignment->setUser($user);
        
        $task = new Task();
        $task->setId($rowdata->task_id);
        $task->setIsLoaded(false);
        $taskassignment->setTask($task);
        
        $taskassignment->setIsLoaded(true);

        return $taskassignment;
    }
    
}
